package com.dtw.ditouwang.ui.more;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.EditorChooseBean;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.ui.BaseFragment;
import com.dtw.ditouwang.ui.WebActivity;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MoreEditChooseFragment extends BaseFragment implements ExpandableListView.OnChildClickListener{
    ExpandableListView expandableListView;
    EditorChooseBean editorChooseBean;
    View headView;
    int type=1;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more_editchoose,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        expandableListView = view.findViewById(R.id.expandableListView);
        expandableListView.setOnChildClickListener(this);

        webApi.getEditorChoose(1).enqueue(new Callback<EditorChooseBean>() {
            @Override
            public void onResponse(Call<EditorChooseBean> call, Response<EditorChooseBean> response) {
                if (response.body().isResult()&&getActivity()!=null&&!getActivity().isFinishing()) {
                    editorChooseBean = response.body();
                    MoreEditorChoooseAdapter moreEditorChoooseAdapter = new MoreEditorChoooseAdapter(response.body());
                    expandableListView.setAdapter(moreEditorChoooseAdapter);
                    if (expandableListView.getHeaderViewsCount() < 1 && response.body().getData() != null) {
                        headView = LayoutInflater.from(getContext()).inflate(R.layout.item_editor_choose_ad, null);
                        expandableListView.addHeaderView(headView);
                    }
                    Banner banner = headView.findViewById(R.id.banner);
                    banner.setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            Glide.with(imageView).load(WebApi.BaseUrl+((EditorChooseBean.DataBeanXX.AdBean)path).getBackground().replace("\\","/")).into(imageView);
                        }
                    });
                    banner.setImages(response.body().getData().getAd());
                    banner.setOnBannerListener(new OnBannerListener() {
                        @Override
                        public void OnBannerClick(int position) {
                            Intent intent = new Intent(getContext(), WebActivity.class);
                            intent.putExtra(WebActivity.KeyUrl, editorChooseBean.getData().getAd().get(position).getLink());
                            startActivity(intent);
                        }
                    });
                    banner.startAutoPlay();
                    banner.start();

                    for (int i = 0; i < response.body().getData().getData().size(); i++) {
                        expandableListView.expandGroup(i);
                    }
                    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                        @Override
                        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                            return true;
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<EditorChooseBean> call, Throwable t) {

            }
        });
    }

    @Override
    public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
        if (groupPosition == 1) {
            Intent intent = new Intent(getContext(), EditorChooseNormalActivity.class);
            intent.putExtra(EditorChooseNormalActivity.KeyID, editorChooseBean.getData().getData().get(groupPosition).getData().get(childPosition).getId());
            intent.putExtra(EditorChooseNormalActivity.KeyType, type);
            intent.putExtra(EditorChooseNormalActivity.KeyTitle, editorChooseBean.getData().getData().get(groupPosition).getData().get(childPosition).getName());
            startActivity(intent);
        } else {
            Intent intent = new Intent(getContext(), EditorChoosePhotoActivity.class);
            intent.putExtra(EditorChoosePhotoActivity.KeyType, type);
            startActivity(intent);
        }
        return true;
    }

    public static void main(String[] args){
        System.out.println("6E:89:F9:D1:ED:0A:06:22:E5:78:EE:4D:86:38:F0:2F"
                .replace(":",""));
    }
}
